//
//  ViewController.swift
//  MaakuMarkUI
//
//  Created by nowcoder on 2025/2/18.
//

import UIKit
import SnapKit

class ViewController: UIViewController {
    
    // MARK: - Properties
    private lazy var tableView: UITableView = {
        let table = UITableView(frame: .zero, style: .plain)
        table.register(MarkdownTestCell.self, forCellReuseIdentifier: MarkdownTestCell.reuseIdentifier)
        table.separatorStyle = .none
        table.backgroundColor = .systemGroupedBackground
        table.contentInset = UIEdgeInsets(top: 8, left: 0, bottom: 8, right: 0)
        table.delegate = self
        table.dataSource = self
        return table
    }()
    
    private let testCases: [(title: String, content: String, type: MarkdownTestType)] = [
        ("标题测试", TestMarkdownData.headingData, .heading),
        ("文本格式化测试", TestMarkdownData.formattingData, .formatting),
        ("列表测试", TestMarkdownData.listData, .list),
        ("链接和图片测试", TestMarkdownData.linkAndImageData, .linkAndImage),
        ("代码块测试", TestMarkdownData.codeBlockData, .codeBlock),
        ("引用测试", TestMarkdownData.blockquoteData, .blockquote),
        ("表格测试", TestMarkdownData.tableData, .table),
        ("分隔线测试", TestMarkdownData.horizontalRuleData, .horizontalRule),
        ("混合格式测试", TestMarkdownData.mixedData, .mixed),
        ("特殊字符测试", TestMarkdownData.specialCharactersData, .specialCharacters),
        ("自定义标签测试", TestMarkdownData.customtagData, .customTag)
    ]
    
    // MARK: - Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
    }
    
    // MARK: - Setup
    private func setupUI() {
        title = "Markdown 测试"
        view.backgroundColor = .systemBackground
        
        // Add tableView
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.edges.equalTo(view.safeAreaLayoutGuide)
        }
    }
}

// MARK: - UITableViewDataSource
extension ViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return testCases.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(
            withIdentifier: MarkdownTestCell.reuseIdentifier,
            for: indexPath
        ) as? MarkdownTestCell else {
            return UITableViewCell()
        }
        
        let testCase = testCases[indexPath.row]
        cell.configure(with: testCase.title, content: testCase.content, type: testCase.type)
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }
}

// MARK: - UITableViewDelegate
extension ViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        let testCase = testCases[indexPath.row]
        let detailVC = MarkdownDetailViewController(title: testCase.title, content: testCase.content)
        navigationController?.pushViewController(detailVC, animated: true)
    }
}
